package com.study.zzedu.ui.classchat;

import android.content.Context;
import android.text.TextUtils;

import com.study.zzedu.base.BaseModel;
import com.study.zzedu.http.OnResultListener;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

public class ClassChatModel extends BaseModel {

    public void getChatInfoList(Context context, String beforeOrAfter,String onlyTeacher,String dataTime,final OnResultListener resultListener){
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/classDiscussion/queryDiscussion",
                new String[]{"beforeOrAfter","onlyTeacher","datetime","pageNum","pageSize"}, new String[]{beforeOrAfter,onlyTeacher,dataTime,"1","10"}, new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                JSONArray jArray = jObject.getJSONArray("data");
                                LogUtil.e("Forget", "查询消息list成功: 消息"+jArray.length()+"条");
                                resultListener.onSuccess(obj);
                            } else {
                                LogUtil.e("Forget", "查询消息list失败");
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        //resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        LogUtil.e("Forget", "查询消息list失败");
                        resultListener.onError(msg);
                    }
                });
    }
    public void sendData(Context context, String content,final OnResultListener resultListener){
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/classDiscussion/sendAComment",
                new String[]{"content"}, new String[]{content}, new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                LogUtil.e("Forget", "发送数据成功");
                                resultListener.onSuccess(obj);
                            } else {
                                LogUtil.e("Forget", "发送数据失败");
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        //resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        LogUtil.e("Forget", "发送数据失败");
                        resultListener.onError(msg);
                    }
                });
    }

}

